<template>
  <div>
    <Float :profit="profit" :vid="vid" v-if="freeorno ==0"></Float>
    <HeadOptions></HeadOptions>
    <Video
      :theurl="theurl"
      :fengmian="fengmian"
      :buyorno="buyorno"
      :freeorno="freeorno"
      v-on:listerson="fatervideo"
    ></Video>
    <p class="video_title">{{title}}</p>
    <div class="vedio_info">
      <span class="vedio_info_text">
        <WatchAndWrite :read_num="read_num"></WatchAndWrite>
        <p>{{create_time}}</p>
      </span>
      <Options :collection="collection" :vid="vid"></Options>
    </div>
    <div class="vediocontent" v-html="detailHtml"></div>

    <BottomBtn
      :xianjia="xianjia"
      :yuanjia="yuanjia"
      v-on:listersonpay="parentpay"
      v-if="buyorno == 1 || freeorno ==0"
    ></BottomBtn>
  </div>
</template>

<script>
  import WatchAndWrite from './../DetailOfBoom/components/WatchAndWrite'
  import BottomBtn from './../DetailOfBoom/components/BottomBtn'
  import Options from './../DetailOfBoom/components/Options'
  import HeadOptions from './../DetailOfBoom/components/HeadOptions'
  import Float from './components/Float'
  import Video from './../../components/Video'
  import * as fetch from '@/api/fetch'

  import Vue from 'vue'
  import { Toast } from 'vant'
  Vue.use(Toast)

  import { weixinConfig, chooseWXPay } from '@/api/wechatPay'

  export default {
    name: 'DetailOfVedio',
    data() {
      return {
        vid: '',
        title: '',
        content: '',
        create_time: '',
        read_num: '',
        collection: '',
        profit: 0,
        shop_price: '',
        theurl: '',
        xianjia: '',
        yuanjia: '',

        buyorno: '',
        freeorno: '',
        fengmian: '',

        inviter: 0,
        spread_id: 0,
      }
    },
    components: {
      WatchAndWrite,
      BottomBtn,
      Options,
      Float,
      HeadOptions,
      Video,
    },
    computed: {
      detailHtml: {
        get() {
          return this.content
        },
        set(val) {
          this.content = val.replace(/\&nbsp;/g, '')
        },
      },
    },
    methods: {
      async getVideoInfo() {
        const res = await fetch.getVideoInfo({
          vid: this.$route.query.vid,
          page: 1,
        })
        if (res && res.errcode == 0) {
          this.title = res.data.title
          this.detailHtml = res.data.content

          this.create_time = res.data.create_time
          this.read_num = res.data.read_num
          this.collection = res.data.collection
          this.profit = res.data.profit
          this.vid = res.data.vid
          this.theurl = res.data.video
          this.buyorno = res.data.is_buy
          this.freeorno = res.data.is_free
          this.fengmian = res.data.picture
          this.xianjia = res.data.market_price
          this.yuanjia = res.data.shop_price
        }
      },

      parentpay() {
        this.processOrder().then(res => {
          if (res.errcode == 0) {
            weixinConfig(res.data.signPackage)
            chooseWXPay(res.data.jsapi, {
              success: function(res) {
                Toast.success('支付成功')
                window.location.reload()
              },
              cancel() {
                alert('已取消支付')
              },
              fail() {
                alert('购买失败，请重新创建订单')
              },
            })
          }
        })
      },

      async playVideoLog() {
        const res = await fetch.playVideoLog({
          vid: this.$route.query.vid,
          length: 0,
        })
      },

      async processOrder() {
        const res = await fetch.processOrder({
          vid: this.$route.query.vid,
          inviter: this.inviter,
          spread_id: this.spread_id,
        })

        return res
      },

      fatervideo() {
        Toast('成功购买即可观看视频')
      },
    },
    mounted() {},
    activated() {
      this.getVideoInfo()
      this.playVideoLog()

      if (
        this.$route.query.inviter == undefined &&
        this.$route.query.spread_id == undefined
      ) {
        this.inviter = 0
        this.spread_id = 0
      } else {
        this.inviter = this.$route.query.inviter
        this.spread_id = this.$route.query.spread_id
        this.saveUserInvitation()
      }
    },
  }
</script>
<style>
  .vediocontent img {
    width: 100%;
  }
  .vediocontent section {
    width: 100%;
  }
</style>

<style lang="less" scoped>
  /deep/ section {
    box-sizing: border-box;
  }
  .thevedio {
    width: 100%;
    height: 282px;
    margin-top: 40px;
  }
  .video_title {
    margin-right: 15px;
    margin-top: 15px;
    margin-left: 15px;
    font-size: 18px;
    line-height: 25px;
  }
  .vedio_info {
    margin-left: 15px;
    margin-right: 15px;
    margin-top: 10px;
    display: flex;
    flex-direction: row;
    .vedio_info_text {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      flex-grow: 1;
      font-size: 13px;
      color: #acacac;
    }
  }

  .vediocontent {
    margin: 15px 0px 60px 0;
  }
</style>
